#include "InFormatter.h"


InFormatter::InFormatter(IntegerFormatter const & formatter, int & value) : _formatter(formatter), _value(value)
{
}


InFormatter::~InFormatter(void)
{
}

std::istream & operator>>(std::istream & stream, InFormatter & formatter)
{
    int work = 0;
    int multi = 1;
    char temp;
    bool found = false;
    while(stream.get(temp))
    {
        if(temp == '-')
        {
            multi *= -1;
        }
        else if(temp == '+')
        {
            multi *= 1;
        }
        else if(!isspace(temp))
        {
            stream.putback(temp);
            break;
        }
    }
    try
    {
        while(stream.get(temp))
        {
            int value = formatter._formatter.valueDigit(temp);
            work *= formatter._formatter._base;
            work += multi * value;
            found = true;
        }
    }
    catch(std::runtime_error e)
    {
    }
    if(found)
    {
        formatter._value = work;
    }
    return stream;
}